import { Component } from "@angular/core"
import { AlertController,NavParams } from 'ionic-angular'
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';
import { AppConfig } from '../../../app/app.config';
@Component({
    selector: 'page-reset',
    templateUrl: 'paypwd.html',
})

export class PaypwdPage{
    firstpwd= 'password';
    againpwd= 'password';
    verify_time = 0;
    userInfo:any;
    form = {
        newpassword:'',
        oldpassword:'',
        captcha:'',
    };
    constructor(
        private http: HttpClient,
        private alertCtrl:AlertController,
        private storage:NativeStorage,
        private navParams:NavParams,
        private translate: TranslateService,
    ) {
        this.userInfo = this.navParams.get('userInfo');
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.storage.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    toggle(index){
        if(index===0){
            if(this.firstpwd=='password'){
                this.firstpwd = 'text'
            }else{
                this.firstpwd = 'password'
            }
        }
        if(index===1){
            if(this.againpwd=='password'){
                this.againpwd = 'text'
            }else{
                this.againpwd = 'password'
            }
        }
    }
    goCode(){
        if(this.verify_time===0){
            this.http.post(AppConfig.getUrl()+'tool/sendcaptcha', AppConfig.urlEcode({receiver:this.userInfo.email}),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( (res) => {
                if(res['code']===101){
                    this.alertb('success','send success,please wait');
                    this.verify_time = 60
                    let timer =setInterval(() => {
                        if(this.verify_time!==0){
                            this.verify_time--
                        }else{
                            clearInterval(timer)
                        }
                    }, 1000);
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
        }else{
            this.alertb('Error','Verification code has been sent, please try again later');
        }
        
    }
    goSub(){
        this.http.post(AppConfig.getUrl()+'user/paypwd', this.form,{headers: new HttpHeaders({ 'Content-Type': 'application/json' }) })
            .subscribe( (res) => {
                if(res['code']===101){
                    this.alertb('success','reset password success');
                }else {
                    this.alertb('Error!',res['msg']);
                }
            })
        
    }
}